/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/3/5
 * \* Time: 14:56
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */

class Node{
    public int val;
    public Node left;
    public Node right;
    public Node(int val){
        this.val = val;
    }
}

public class BinarySearchTree {
    public Node root = null;

    public Node search(int key){
        Node cur = root;
        while (cur != null){
            if(cur.val == key){
                return cur;
            }else if(cur.val < key){
                cur = cur.right;
            }else {
                cur = cur.left;
            }
        }
        return null;//表示没有这个结构
    }

    public boolean insert(int val){
        if(root == null){
            root = new Node(val);
            return true;
        }
        Node cur = root;
        Node parent = null;
        while(cur != null){
            if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val == val){
                return false;//不能存在相同的元素
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
        Node node = new Node(val);
        if(parent.val < val){
            parent.right = node;
        }else {
            parent.left = node;
        }
        return true;
    }

    //遍历寻找key的位置
    public void remove(int key){
        Node cur = root;
        Node parent = null;
        while(cur != null){
            if(cur.val == key){
                removeNode(cur,parent);//调用函数
                break;
            }else if(cur.val<key){
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
    }
//真正的删除结点函数
    public void removeNode(Node cur,Node parent){
        if(cur.left == null){//第一种情况，左子树为空
            if(cur == root){
                root = cur.right;
            }else if(cur == parent.left){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;

            }
        }else if(cur.right == null){//第二种情况，右子树为空
            if(cur == root){
                root = cur.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else{//第三种情况，左右子树均不为空
            Node targetParent = cur;
            Node target = cur.right;
            while(target.left != null){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            targetParent.left = target.right;
            if(target == targetParent.left){//target在左边的情况
                targetParent.left = target.right;
            }else{//target在右边的情况
                targetParent.right = target.right;
            }
        }
    }
//中序遍历
    public void inOrder(Node root){
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val+ " ");
        inOrder(root.right);
    }



    public static void main(String[] args) {
        int[] array = {31,2,18,19,9,34,7};
        BinarySearchTree binarySearchTree = new BinarySearchTree();//实例化
        for(int i = 0 ;i < array.length; i++){
            binarySearchTree.insert(array[i]);//插入结点

        }
        binarySearchTree.inOrder(binarySearchTree.root);//中序遍历
        System.out.println();
        System.out.println("插入重复的数据");
        System.out.println(binarySearchTree.insert(3));//数据不存在，可插入，返回true
        System.out.println("删除数据：");
        binarySearchTree.remove(7);;
        binarySearchTree.inOrder(binarySearchTree.root);//中序遍历
    }
}
